package com.ifox.frame;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Frame;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Vector;

import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import org.apache.commons.lang.StringUtils;

import com.ifox.common.ConnectTypes;
import com.ifox.common.MyDefaultTableModel;
import com.ifox.entity.IFox;
import com.ifox.entity.IFoxTable;
import com.ifox.service.BaseService;
import com.ifox.util.Utils;

public class DataSourceFrame extends JDialog {

	private BaseService service = new BaseService();
	
	private static final long serialVersionUID = 7463480272647380698L;
	private JPanel contentPane;
	private JTextField txtIfName;
	private JTextField txtSleepTime;
	private JTextField txtSrcHostIp;
	private JTextField txtSrcPort;
	private JTextField txtSrcDbName;
	private JTextField txtSrcUsername;
	private JPasswordField txtSrcPwd;
	private JTextField txtSrcCharset;
	private JTextField txtTargetHostIp;
	private JTextField txtTargetPort;
	private JTextField txtTargetDbName;
	private JTextField txtTargetUsername;
	private JPasswordField txtTargetPwd;
	private JTextField txtTargetCharset;
	private JTextField txtGetCount;
	private JTextField txtSrcShowname;
	private JTextField txtTargetShowname;
	private JTextField txtSrcTestSql;
	private JTextField txtTargetTestSql;
	private JComboBox cmbSrcConnType;
	private JComboBox cmbTargetConnType;
	private JTable tbDbMapping;
	private JMenuItem mbtnAdd;
	private JMenuItem mbtnEdit;
	private JMenuItem mbtnDel;
	public DataSourceFrame(Frame owner, boolean modal) throws HeadlessException {
        super(owner,null,modal);
        addWindowListener(new WindowAdapter() {
        	@Override
        	public void windowClosed(WindowEvent e) {
        		ListFrame.ifox = null;
        	}
        });
		init();
    }
	
	private void init(){
		setModal(true);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setTitle("交换接口配置");
		setResizable(false);
		setBounds(100, 100, 932, 514);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JToolBar toolBar = new JToolBar();
		toolBar.setFloatable(false);
		contentPane.add(toolBar, BorderLayout.NORTH);
		
		JButton btnTest = new JButton("测试连接");
		btnTest.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String validResult = valid();
				if(StringUtils.isNotBlank(validResult)){
					JOptionPane.showMessageDialog(null, validResult, "信息", JOptionPane.WARNING_MESSAGE);
				}else{
					String result = service.getTestConnectionResult(getIFox());
					JOptionPane.showMessageDialog(null, result, "信息", JOptionPane.INFORMATION_MESSAGE);
				}
			}
		});
		
		JButton btnSave = new JButton("保存");
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String result = "";
				if(ListFrame.isAdd){
					result = valid();
				}else{
					try {
						Integer.parseInt(txtSleepTime.getText());
					} catch (Exception ex) {
						result += "抓取间隔必须是整数！\n";
					}
					try {
						Integer.parseInt(txtGetCount.getText());
					} catch (Exception ex) {
						result += "抓取数量必须是整数！\n";
					}
				}
				
				if(StringUtils.isNotBlank(result)){
					JOptionPane.showMessageDialog(null, result, "信息", JOptionPane.WARNING_MESSAGE);
				}else{
					IFox ifox = getIFox();
					if(ListFrame.isAdd && Utils.isExists(ifox.getIfoxName())){
						JOptionPane.showMessageDialog(null, "配置文件已存在，请更改【接口名称】！", "信息", JOptionPane.WARNING_MESSAGE);
					}else{
						Utils.saveIFox(ifox);
						DataSourceFrame.this.dispose();
					}
				}
			}
		});
		toolBar.add(btnSave);
		toolBar.add(btnTest);
		
		JButton btnExit = new JButton("关闭");
		btnExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				DataSourceFrame.this.dispose();
			}
		});
		toolBar.add(btnExit);
		
		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.CENTER);
		panel.setLayout(null);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new TitledBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)), "\u6E90\u6570\u636E\u5E93", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		panel_1.setBounds(10, 108, 439, 166);
		panel.add(panel_1);
		panel_1.setLayout(null);
		
		JLabel lblip = new JLabel("主机IP：");
		lblip.setHorizontalAlignment(SwingConstants.RIGHT);
		lblip.setBounds(32, 27, 48, 15);
		panel_1.add(lblip);
		
		txtSrcHostIp = new JTextField();
		txtSrcHostIp.setBounds(90, 24, 120, 21);
		panel_1.add(txtSrcHostIp);
		txtSrcHostIp.setColumns(10);
		
		txtSrcPort = new JTextField();
		txtSrcPort.setColumns(10);
		txtSrcPort.setBounds(309, 24, 120, 21);
		panel_1.add(txtSrcPort);
		
		JLabel label_2 = new JLabel("端口：");
		label_2.setHorizontalAlignment(SwingConstants.RIGHT);
		label_2.setBounds(251, 27, 48, 15);
		panel_1.add(label_2);
		
		JLabel label_3 = new JLabel("数据库名：");
		label_3.setHorizontalAlignment(SwingConstants.RIGHT);
		label_3.setBounds(20, 55, 60, 15);
		panel_1.add(label_3);
		
		txtSrcDbName = new JTextField();
		txtSrcDbName.setColumns(10);
		txtSrcDbName.setBounds(90, 52, 120, 21);
		panel_1.add(txtSrcDbName);
		
		JLabel label_4 = new JLabel("连接类型：");
		label_4.setHorizontalAlignment(SwingConstants.RIGHT);
		label_4.setBounds(239, 55, 60, 15);
		panel_1.add(label_4);
		
		JLabel label_5 = new JLabel("用户名：");
		label_5.setHorizontalAlignment(SwingConstants.RIGHT);
		label_5.setBounds(32, 83, 48, 15);
		panel_1.add(label_5);
		
		txtSrcUsername = new JTextField();
		txtSrcUsername.setColumns(10);
		txtSrcUsername.setBounds(90, 80, 120, 21);
		panel_1.add(txtSrcUsername);
		
		JLabel label_6 = new JLabel("字符集：");
		label_6.setHorizontalAlignment(SwingConstants.RIGHT);
		label_6.setBounds(228, 83, 71, 15);
		panel_1.add(label_6);
		
		cmbSrcConnType = new JComboBox();
		cmbSrcConnType.setModel(new DefaultComboBoxModel(ConnectTypes.values()));
		cmbSrcConnType.setBounds(309, 52, 120, 21);
		panel_1.add(cmbSrcConnType);
		
		txtSrcPwd = new JPasswordField();
		txtSrcPwd.setBounds(90, 108, 120, 21);
		panel_1.add(txtSrcPwd);
		
		JLabel label_13 = new JLabel("密码：");
		label_13.setHorizontalAlignment(SwingConstants.RIGHT);
		label_13.setBounds(32, 111, 48, 15);
		panel_1.add(label_13);
		
		txtSrcCharset = new JTextField();
		txtSrcCharset.setColumns(10);
		txtSrcCharset.setBounds(309, 80, 120, 21);
		panel_1.add(txtSrcCharset);
		
		JLabel label_16 = new JLabel("显示名：");
		label_16.setHorizontalAlignment(SwingConstants.RIGHT);
		label_16.setBounds(251, 111, 48, 15);
		panel_1.add(label_16);
		
		txtSrcShowname = new JTextField();
		txtSrcShowname.setColumns(10);
		txtSrcShowname.setBounds(309, 108, 120, 21);
		panel_1.add(txtSrcShowname);
		
		JLabel label_18 = new JLabel("测试语句：");
		label_18.setHorizontalAlignment(SwingConstants.RIGHT);
		label_18.setBounds(20, 138, 60, 15);
		panel_1.add(label_18);
		
		txtSrcTestSql = new JTextField();
		txtSrcTestSql.setColumns(10);
		txtSrcTestSql.setBounds(90, 135, 339, 21);
		panel_1.add(txtSrcTestSql);
		
		JPanel panel_3 = new JPanel();
		panel_3.setBorder(new TitledBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)), "\u540D\u79F0", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		panel_3.setBounds(10, 10, 439, 88);
		panel.add(panel_3);
		panel_3.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("接口名称：");
		lblNewLabel.setBounds(10, 25, 60, 15);
		panel_3.add(lblNewLabel);
		
		txtIfName = new JTextField();
		txtIfName.setBounds(80, 22, 349, 21);
		panel_3.add(txtIfName);
		txtIfName.setColumns(10);
		
		JLabel label = new JLabel("抓取间隔：");
		label.setBounds(10, 56, 65, 15);
		panel_3.add(label);
		
		txtSleepTime = new JTextField();
		txtSleepTime.setBounds(80, 53, 89, 21);
		panel_3.add(txtSleepTime);
		txtSleepTime.setColumns(10);
		
		JLabel label_1 = new JLabel("（单位：秒）");
		label_1.setBounds(168, 56, 77, 15);
		panel_3.add(label_1);
		
		JLabel label_15 = new JLabel("抓取数量：");
		label_15.setBounds(270, 56, 65, 15);
		panel_3.add(label_15);
		
		txtGetCount = new JTextField();
		txtGetCount.setColumns(10);
		txtGetCount.setBounds(340, 53, 89, 21);
		panel_3.add(txtGetCount);
		
		JPanel panel_4 = new JPanel();
		panel_4.setLayout(null);
		panel_4.setBorder(new TitledBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)), "\u76EE\u6807\u6570\u636E\u5E93", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		panel_4.setBounds(10, 280, 439, 166);
		panel.add(panel_4);
		
		JLabel label_7 = new JLabel("主机IP：");
		label_7.setHorizontalAlignment(SwingConstants.RIGHT);
		label_7.setBounds(32, 27, 48, 15);
		panel_4.add(label_7);
		
		txtTargetHostIp = new JTextField();
		txtTargetHostIp.setColumns(10);
		txtTargetHostIp.setBounds(90, 24, 120, 21);
		panel_4.add(txtTargetHostIp);
		
		txtTargetPort = new JTextField();
		txtTargetPort.setColumns(10);
		txtTargetPort.setBounds(309, 24, 120, 21);
		panel_4.add(txtTargetPort);
		
		JLabel label_8 = new JLabel("端口：");
		label_8.setHorizontalAlignment(SwingConstants.RIGHT);
		label_8.setBounds(251, 27, 48, 15);
		panel_4.add(label_8);
		
		JLabel label_9 = new JLabel("数据库名：");
		label_9.setHorizontalAlignment(SwingConstants.RIGHT);
		label_9.setBounds(20, 55, 60, 15);
		panel_4.add(label_9);
		
		txtTargetDbName = new JTextField();
		txtTargetDbName.setColumns(10);
		txtTargetDbName.setBounds(90, 52, 120, 21);
		panel_4.add(txtTargetDbName);
		
		JLabel label_10 = new JLabel("连接类型：");
		label_10.setHorizontalAlignment(SwingConstants.RIGHT);
		label_10.setBounds(239, 55, 60, 15);
		panel_4.add(label_10);
		
		JLabel label_11 = new JLabel("用户名：");
		label_11.setHorizontalAlignment(SwingConstants.RIGHT);
		label_11.setBounds(32, 83, 48, 15);
		panel_4.add(label_11);
		
		txtTargetUsername = new JTextField();
		txtTargetUsername.setColumns(10);
		txtTargetUsername.setBounds(90, 80, 120, 21);
		panel_4.add(txtTargetUsername);
		
		JLabel label_12 = new JLabel("字符集：");
		label_12.setHorizontalAlignment(SwingConstants.RIGHT);
		label_12.setBounds(228, 83, 71, 15);
		panel_4.add(label_12);
		
		cmbTargetConnType = new JComboBox();
		cmbTargetConnType.setModel(new DefaultComboBoxModel(ConnectTypes.values()));
		cmbTargetConnType.setBounds(309, 52, 120, 21);
		panel_4.add(cmbTargetConnType);
		
		txtTargetPwd = new JPasswordField();
		txtTargetPwd.setBounds(90, 108, 120, 21);
		panel_4.add(txtTargetPwd);
		
		JLabel label_14 = new JLabel("密码：");
		label_14.setHorizontalAlignment(SwingConstants.RIGHT);
		label_14.setBounds(32, 111, 48, 15);
		panel_4.add(label_14);
		
		txtTargetCharset = new JTextField();
		txtTargetCharset.setColumns(10);
		txtTargetCharset.setBounds(309, 80, 120, 21);
		panel_4.add(txtTargetCharset);
		
		txtTargetShowname = new JTextField();
		txtTargetShowname.setColumns(10);
		txtTargetShowname.setBounds(309, 108, 120, 21);
		panel_4.add(txtTargetShowname);
		
		JLabel label_17 = new JLabel("显示名：");
		label_17.setHorizontalAlignment(SwingConstants.RIGHT);
		label_17.setBounds(228, 111, 71, 15);
		panel_4.add(label_17);
		
		JLabel label_19 = new JLabel("测试语句：");
		label_19.setHorizontalAlignment(SwingConstants.RIGHT);
		label_19.setBounds(20, 138, 60, 15);
		panel_4.add(label_19);
		
		txtTargetTestSql = new JTextField();
		txtTargetTestSql.setColumns(10);
		txtTargetTestSql.setBounds(90, 135, 339, 21);
		panel_4.add(txtTargetTestSql);
		
		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new TitledBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)), "\u6570\u636E\u5E93\u6620\u5C04\u89C4\u5219", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		panel_2.setBounds(459, 10, 457, 433);
		panel.add(panel_2);
		panel_2.setLayout(new BorderLayout(0, 0));
		
		JScrollPane scrollPane = new JScrollPane();
		panel_2.add(scrollPane, BorderLayout.CENTER);
		
		JPopupMenu popupMenu = new JPopupMenu();
		
		mbtnAdd = new JMenuItem("添加");
		mbtnAdd.setIcon(new ImageIcon(DataSourceFrame.class.getResource("/com/ifox/res/edit_add.png")));
		mbtnAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				MappingFrame frame = new MappingFrame(DataSourceFrame.this,true,null,null);
				frame.setLocationRelativeTo(null);
				frame.setVisible(true);
				refreshTable();
			}
		});
		popupMenu.add(mbtnAdd);
		
		mbtnEdit = new JMenuItem("修改");
		mbtnEdit.setIcon(new ImageIcon(DataSourceFrame.class.getResource("/com/ifox/res/pencil.png")));
		mbtnEdit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int row = tbDbMapping.getSelectedRow();
				if(row<0){
					JOptionPane.showMessageDialog(null, "请先选中一行再使用该功能！" , "信息", JOptionPane.WARNING_MESSAGE);
				}else{
					String src = tbDbMapping.getValueAt(row, 0).toString();
					String target = tbDbMapping.getValueAt(row, 1).toString();
					MappingFrame frame = new MappingFrame(DataSourceFrame.this,true,src,target);
					frame.setLocationRelativeTo(null);
					frame.setVisible(true);
					refreshTable();
				}
			}
		});
		popupMenu.add(mbtnEdit);
		
		mbtnDel = new JMenuItem("删除");
		mbtnDel.setIcon(new ImageIcon(DataSourceFrame.class.getResource("/com/ifox/res/cancel.png")));
		mbtnDel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int row = tbDbMapping.getSelectedRow();
				String src = tbDbMapping.getValueAt(row, 0).toString();
				String target = tbDbMapping.getValueAt(row, 1).toString();
				ListFrame.ifox.removeTable(src, target);
				DefaultTableModel model = (DefaultTableModel)tbDbMapping.getModel();
				model.removeRow(tbDbMapping.getSelectedRow());
			}
		});
		popupMenu.add(mbtnDel);
		
		tbDbMapping = new JTable();
		tbDbMapping.getTableHeader().setReorderingAllowed(false);
		tbDbMapping.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		scrollPane.setViewportView(tbDbMapping);
		
		setIFox();
		refreshTable();
		addPopup(scrollPane, popupMenu);
		addPopup(tbDbMapping, popupMenu);
	}

	
	private Vector<String> getHeader(){
		String[] columnNames = { "发出表名", "目标表名", "状态列" };
		Vector<String> header = new Vector<String>();

		for (int i = 0; i < columnNames.length; i++) {
			header.add(columnNames[i]);
		}
		return header;
	}
	
	private Vector<Vector<String>> getRow(){
		Vector<Vector<String>> row = new Vector<Vector<String>>();
		Vector<String> cell;
		for (IFoxTable table : ListFrame.ifox.getTables()) {
			cell = new Vector<String>();
			cell.add(table.getSrcTable());
			cell.add(table.getTargetTable());
			cell.add(table.getStatusColumn());
			row.add(cell);
		}
		return row;
	}
	
	private TableModel getTableModel(){
		MyDefaultTableModel tablmod = new MyDefaultTableModel();
		tablmod.setDataVector(getRow(), getHeader());
		return tablmod;
	}
	
	private void refreshTable(){
		tbDbMapping.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		tbDbMapping.setModel(getTableModel());
		tbDbMapping.getColumnModel().getColumn(0).setPreferredWidth(170);
		tbDbMapping.getColumnModel().getColumn(1).setPreferredWidth(170);
		tbDbMapping.getColumnModel().getColumn(2).setPreferredWidth(100);
		tbDbMapping.validate();
		tbDbMapping.repaint(); 
	}
	
	
	
	private void setIFox(){
		txtIfName.setText(ListFrame.ifox.getIfoxName());
		if(!ListFrame.isAdd){
			txtIfName.setEditable(false);
		}
		txtSleepTime.setText(ListFrame.ifox.getSleepTime()+"");
		txtGetCount.setText(ListFrame.ifox.getFetchCount()+"");
		txtSrcHostIp.setText(ListFrame.ifox.getSrcIp());
		txtSrcPort.setText(ListFrame.ifox.getSrcPort());
		txtSrcDbName.setText(ListFrame.ifox.getSrcDb());
		cmbSrcConnType.setSelectedItem(ConnectTypes.valueOf(ListFrame.ifox.getSrcConnType()));
		txtSrcUsername.setText(ListFrame.ifox.getSrcUsername());
		txtSrcCharset.setText(ListFrame.ifox.getSrcCharset());
		txtSrcPwd.setText(ListFrame.ifox.getSrcPassword());
		txtSrcShowname.setText(ListFrame.ifox.getSrcShowname());
		txtSrcTestSql.setText(ListFrame.ifox.getSrcTestSql());
		
		txtTargetHostIp.setText(ListFrame.ifox.getTargetIp());
		txtTargetPort.setText(ListFrame.ifox.getTargetPort());
		txtTargetDbName.setText(ListFrame.ifox.getTargetDb());
		cmbTargetConnType.setSelectedItem(ConnectTypes.valueOf(ListFrame.ifox.getTargetConnType()));
		txtTargetUsername.setText(ListFrame.ifox.getTargetUsername());
		txtTargetCharset.setText(ListFrame.ifox.getTargetCharset());
		txtTargetPwd.setText(ListFrame.ifox.getTargetPassword());
		txtTargetShowname.setText(ListFrame.ifox.getTargetShowname());
		txtTargetTestSql.setText(ListFrame.ifox.getTargetTestSql());
	}
	
	private IFox getIFox(){
		IFox ifox = ListFrame.ifox;
		ifox.setIfoxName(txtIfName.getText());
		ifox.setSleepTime(Integer.parseInt(txtSleepTime.getText()));
		ifox.setFetchCount(Integer.parseInt(txtGetCount.getText()));
		ifox.setSrcIp(txtSrcHostIp.getText());
		ifox.setSrcPort(txtSrcPort.getText());
		ifox.setSrcDb(txtSrcDbName.getText());
		ifox.setSrcConnType(cmbSrcConnType.getSelectedItem().toString());
		ifox.setSrcUsername(txtSrcUsername.getText());
		ifox.setSrcCharset(txtSrcCharset.getText());
		ifox.setSrcPassword(new String(txtSrcPwd.getPassword()));
		ifox.setSrcShowname(txtSrcShowname.getText());
		ifox.setSrcTestSql(txtSrcTestSql.getText());
		
		ifox.setTargetIp(txtTargetHostIp.getText());
		ifox.setTargetPort(txtTargetPort.getText());
		ifox.setTargetDb(txtTargetDbName.getText());
		ifox.setTargetConnType(cmbTargetConnType.getSelectedItem().toString());
		ifox.setTargetUsername(txtTargetUsername.getText());
		ifox.setTargetCharset(txtTargetCharset.getText());
		ifox.setTargetPassword(new String(txtTargetPwd.getPassword()));
		ifox.setTargetShowname(txtTargetShowname.getText());
		ifox.setTargetTestSql(txtTargetTestSql.getText());
		ListFrame.ifox = ifox;
		return ifox;
	}
	
	private String valid(){
		String result = "";
		if(StringUtils.isBlank(txtIfName.getText())){
			result += "接口名称不能为空！\n";
		}
		if(StringUtils.isBlank(txtSleepTime.getText())){
			result += "抓取间隔不能为空！\n";
		}
		try {
			Integer.parseInt(txtSleepTime.getText());
		} catch (Exception e) {
			result += "抓取间隔必须是整数！\n";
		}
		if(StringUtils.isBlank(txtGetCount.getText())){
			result += "抓取数量不能为空！\n";
		}
		try {
			Integer.parseInt(txtGetCount.getText());
		} catch (Exception e) {
			result += "抓取数量必须是整数！\n";
		}
		
		if(StringUtils.isBlank(txtSrcHostIp.getText()) || StringUtils.isBlank(txtTargetHostIp.getText())){
			result += "主机IP不能为空！\n";
		}
		
		if(StringUtils.isBlank(txtSrcPort.getText()) || StringUtils.isBlank(txtTargetPort.getText())){
			result += "端口不能为空！\n";
		}
		
		if(StringUtils.isBlank(txtSrcDbName.getText()) || StringUtils.isBlank(txtTargetDbName.getText())){
			result += "数据库名不能为空！\n";
		}
		
		if(StringUtils.isBlank(txtSrcUsername.getText()) || StringUtils.isBlank(txtTargetUsername.getText())){
			result += "用户名不能为空！\n";
		}
		
		if(StringUtils.isBlank(new String(txtSrcPwd.getPassword())) || StringUtils.isBlank(new String(txtTargetPwd.getPassword()))){
			result += "密码不能为空！\n";
		}
		
		if(StringUtils.isBlank(txtSrcTestSql.getText()) || StringUtils.isBlank(txtTargetTestSql.getText())){
			result += "测试语句不能为空！\n";
		}
		return result;
	}
	private void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			private void showMenu(MouseEvent e) {
				int row = tbDbMapping.getSelectedRow();
				if(row<0){
					mbtnEdit.setEnabled(false);
					mbtnDel.setEnabled(false);
				}else{
					mbtnEdit.setEnabled(true);
					mbtnDel.setEnabled(true);
				}
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}
}


















